package uk.co.wroedevelopments.semanticworkbench.actions;

import java.util.Iterator;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.xtext.concurrent.IUnitOfWork;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.core.editor.XtextEditor;
import org.eclipse.xtext.ui.core.editor.XtextSourceViewer;
import org.eclipse.xtext.ui.core.editor.model.XtextDocument;
import org.eclipse.jface.dialogs.MessageDialog;
import org.xtext.example.myDsl.MyDslPackage;

/**
 * Our sample action implements workbench action delegate. The action proxy will
 * be created by the workbench and shown in the UI. When the user tries to use
 * the action, this delegate will be created and execution will be delegated to
 * it.
 * 
 * @see IWorkbenchWindowActionDelegate
 */
public class NeoImportAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;
	private ISelection selection;
	private IFile selectedFile;
	private Shell shell;
	private final String NEW_SUB="NewSub";

	/**
	 * The constructor.
	 */
	public NeoImportAction() {
	}

	/**
	 * The action has been activated. The argument of the method represents the
	 * 'real' action sitting in the workbench UI.
	 * 
	 * @see IWorkbenchWindowActionDelegate#run
	 */
	public void run(IAction action) {
		try {

			System.out.println(selectedFile);
			IEditorPart ed = window.getActivePage().getActiveEditor();
			if (ed instanceof XtextEditor) {
				XtextEditor xed = (XtextEditor) ed;
				ISourceViewer sv = xed.getInternalSourceViewer();
				XtextSourceViewer xsv = (XtextSourceViewer) sv;
				XtextDocument doc = (XtextDocument) xsv.getDocument();
				doc.readOnly(new IUnitOfWork.Void<XtextResource>() {
					public void process(XtextResource resource) throws Exception {
						
						IParseResult parseResult = resource.getParseResult();
						EObject root = parseResult.getRootASTElement();
						ModelSwitch ms = new ModelSwitch();
						ms.doSwitch(root);
						
					
					}
						
					});
				System.out.println(doc.getClass().getName());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		MessageDialog.openInformation(window.getShell(), "Semanticworkbench",
				"Imported");
	}

	/**
	 * Selection in the workbench has been changed. We can change the state of
	 * the 'real' action here if we want, but this can only happen after the
	 * delegate has been created.
	 * 
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection selection2 = (IStructuredSelection) selection;
			if (!selection.isEmpty()) {
				Object selection3 = selection2.getFirstElement();
				if (selection3 instanceof IFile) {
					selectedFile = (IFile) selection3;
				}
			}
		}

	}

	/**
	 * We can use this method to dispose of any system resources we previously
	 * allocated.
	 * 
	 * @see IWorkbenchWindowActionDelegate#dispose
	 */
	public void dispose() {
	}

	/**
	 * We will cache window object in order to be able to provide parent shell
	 * for the message dialog.
	 * 
	 * @see IWorkbenchWindowActionDelegate#init
	 */
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}